package eppic.pkg;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.app.*;
import android.content.*;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import 	android.view.LayoutInflater;
import 	android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Calendar;
import android.app.TimePickerDialog;
import java.util.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import android.content.res.Resources;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.graphics.Color;
import android.text.Html;
import android.widget.TableLayout.LayoutParams;


public class HoursTrackerActivity extends Activity {
	
	public Client[] clients;
	public Project[] projects;
	public Project clockInAt_Project;
	public Client clockInAt_Client;
	static final int TIME_DIALOG_ID = 0;
	private int mHour;
    private int mMinute;
	private int mDay;
    private int mMonth;
	private int mYear;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ViewGroup client_box = (ViewGroup) findViewById(R.id.clients);
        ViewGroup project_box = (ViewGroup) findViewById(R.id.projects);

        
        // Get all Clients and Projects
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        clients = dbHelper.getAllClients();
        projects = dbHelper.getAllProjects();
        generateStats(clients, projects);
        
        //Populate Projects
        if(projects.length > 0)
        {
        	for(int i = 0; i < projects.length; i++)
        	{
        		Project x = projects[i]; 
        		TableLayout tl = getProjectRow(this, x, i);
    	        project_box.addView(tl);
        	}

        }
        
        
        //Populate Clients
        if(clients.length > 0)
        {
        	for(int i = 0; i < clients.length; i++)
        	{
            	Client x = clients[i]; 
            	TableLayout tl = getClientRow(this, x, i);
    	        client_box.addView(tl);
        	}

        }
        
        showProjects(null);
       
    }
    
    public TableLayout getProjectRow(Context context, Project project, int i)
    {
    	
    	//Create Table Layout 1
    	TableLayout tl = new TableLayout(this);
    	tl.setPadding(8, 8, 8, 8);
    	

    	TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams();
    	layoutParams.setMargins(0, 12, 0, 12);
    	tl.setLayoutParams(layoutParams);
    	
    	//Add Background
    	Resources res = getApplicationContext().getResources();
        Drawable greyBox = res.getDrawable(R.drawable.inset_grey);
        tl.setBackgroundDrawable(greyBox);
    	
    	
    	//Name for Table 1
    	TextView name = new TextView(context);
    	name.setText(project.name);
    	name.setTextColor(Color.WHITE);
    	name.setTextSize(24);
    	tl.addView(name);
    	
    	//Client Info
    	TextView client = new TextView(context);
    	client.setText(Html.fromHtml("Client: <i>"+project.getClientName(context)+"</i>"));
    	client.setTextColor(Color.WHITE);
    	client.setTextSize(16);
    	tl.addView(client);
    	
    	//Launch Button Table Layout 1
    	ImageButton button = new ImageButton(this);
        Drawable greenBtn = res.getDrawable(R.drawable.green_button);
        Drawable launch = res.getDrawable(R.drawable.launchmenu);
        button.setImageDrawable(launch);
        button.setBackgroundDrawable(greenBtn);
        button.setPadding(0, 3, 0, 0);
        button.setOnClickListener(launch_projectMenu);
        button.setTag(i);
        tl.addView(button);
        
    	return tl;
    }
    
    public TableLayout getClientRow(Context context, Client client, int i)
    {
    	
    	//Create Table Layout 1
    	TableLayout tl = new TableLayout(this);
    	tl.setPadding(8, 8, 8, 8);
    	

    	TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams();
    	layoutParams.setMargins(0, 12, 0, 12);
    	tl.setLayoutParams(layoutParams);
    	
    	//Add Background
    	Resources res = getApplicationContext().getResources();
        Drawable greyBox = res.getDrawable(R.drawable.inset_grey);
        tl.setBackgroundDrawable(greyBox);
    	
    	
    	//Name for Table 1
    	TextView name = new TextView(context);
    	name.setText(client.company);
    	name.setTextColor(Color.WHITE);
    	name.setTextSize(24);
    	tl.addView(name);
    	
    	//Client Info
    	TextView contact = new TextView(context);
    	contact.setText(Html.fromHtml("Contact: <i>"+client.client+"</i>"));
    	contact.setTextColor(Color.WHITE);
    	contact.setTextSize(16);
    	tl.addView(contact);
    	
    	//Launch Button Table Layout 1
    	ImageButton button = new ImageButton(this);
        Drawable greenBtn = res.getDrawable(R.drawable.green_button);
        Drawable launch = res.getDrawable(R.drawable.launchmenu);
        button.setImageDrawable(launch);
        button.setBackgroundDrawable(greenBtn);
        button.setPadding(0, 3, 0, 0);
        button.setOnClickListener(launch_clientMenu);
        button.setTag(i);
        tl.addView(button);
        
    	return tl;
    }
    
    public void showProjects(View view)
    {
    	ViewGroup client_box = (ViewGroup) findViewById(R.id.clients);
        ViewGroup project_box = (ViewGroup) findViewById(R.id.projects);
        ViewGroup stats_box = (ViewGroup) findViewById(R.id.stats);
        TextView ribbon = (TextView) findViewById(R.id.ribbonText);
        Button proj = (Button) findViewById(R.id.projBtn);
        Button client = (Button) findViewById(R.id.clientBtn);
        Button stats = (Button) findViewById(R.id.statsBtn);
        
        client_box.setVisibility(8);
        stats_box.setVisibility(8);
        project_box.setVisibility(0);
        ribbon.setText("Projects");
        
        Resources res = getApplicationContext().getResources();
        Drawable down = res.getDrawable(R.drawable.footer_bg_pressed);
        Drawable up = res.getDrawable(R.drawable.footbar);
        Drawable up2 = res.getDrawable(R.drawable.footbar);
        proj.setBackgroundDrawable(down);
        client.setBackgroundDrawable(up);
        stats.setBackgroundDrawable(up2);
    }
    
    public void showClients(View view)
    {
    	ViewGroup client_box = (ViewGroup) findViewById(R.id.clients);
        ViewGroup project_box = (ViewGroup) findViewById(R.id.projects);
        ViewGroup stats_box = (ViewGroup) findViewById(R.id.stats);
        TextView ribbon = (TextView) findViewById(R.id.ribbonText);
        Button proj = (Button) findViewById(R.id.projBtn);
        Button client = (Button) findViewById(R.id.clientBtn);
        Button stats = (Button) findViewById(R.id.statsBtn);
        
        client_box.setVisibility(0);
        stats_box.setVisibility(8);
        project_box.setVisibility(8);
        ribbon.setText("Clients");
        
        Resources res = getApplicationContext().getResources();
        Drawable down = res.getDrawable(R.drawable.footer_bg_pressed);
        Drawable up = res.getDrawable(R.drawable.footbar);
        Drawable up2 = res.getDrawable(R.drawable.footbar);
        proj.setBackgroundDrawable(up);
        client.setBackgroundDrawable(down);
        stats.setBackgroundDrawable(up2);
    }
    
    public void showStats(View view)
    {
    	ViewGroup client_box = (ViewGroup) findViewById(R.id.clients);
        ViewGroup project_box = (ViewGroup) findViewById(R.id.projects);
        ViewGroup stats_box = (ViewGroup) findViewById(R.id.stats);
        TextView ribbon = (TextView) findViewById(R.id.ribbonText);
        Button proj = (Button) findViewById(R.id.projBtn);
        Button client = (Button) findViewById(R.id.clientBtn);
        Button stats = (Button) findViewById(R.id.statsBtn);
        
        client_box.setVisibility(8);
        stats_box.setVisibility(0);
        project_box.setVisibility(8);
        ribbon.setText("Statistics");
        
        Resources res = getApplicationContext().getResources();
        Drawable down = res.getDrawable(R.drawable.footer_bg_pressed);
        Drawable up = res.getDrawable(R.drawable.footbar);
        Drawable up2 = res.getDrawable(R.drawable.footbar);
        proj.setBackgroundDrawable(up);
        client.setBackgroundDrawable(up2);
        stats.setBackgroundDrawable(down);
    }
    
   public void generateStats(Client[] clients, Project[] projects)
    {
	   int totalLogs = 0;
	   long totalTime = 0;
	   long longestTime = 0;
	   float moneyEarned = 0;
	    
	    int totalExpenses = 0;
	    float expenseCost = 0;
	    
    	TextView money = (TextView) findViewById(R.id.totalearned);
    	TextView expenses = (TextView) findViewById(R.id.totalexpenses);
    	TextView total = (TextView) findViewById(R.id.totalowed);
    	
    	TextView totalwork = (TextView) findViewById(R.id.hrsworked);
    	TextView avgwork = (TextView) findViewById(R.id.avgwork);
    	TextView longwork = (TextView) findViewById(R.id.longestwrk);
    	
    	TextView hourly = (TextView) findViewById(R.id.avghrly);

    	TextView clients_view = (TextView) findViewById(R.id.allclients);
    	TextView projects_view = (TextView) findViewById(R.id.allprojects);
    	TextView logs_view = (TextView) findViewById(R.id.timelogs);
    	TextView expenses_view = (TextView) findViewById(R.id.expensesnum);
    	
    	  if(clients.length > 0)
          {
          	for(int i = 0; i < clients.length; i++)
          	{
              	Client a = clients[i];
              	Expense[] e = a.getExpenses(this);
              	totalExpenses+= e.length;
              	if(e.length > 0)
                {
                	for(int j = 0; j < e.length; j++)
                	{
                		expenseCost+=e[i].cost;
                	}
                }
              	
              	Log[] l = a.getLogs(this);
              	totalLogs += l.length;
              	if(l.length > 0)
                {
                	for(int k = 0; k < l.length; k++)
                	{
                		totalTime += l[k].time();
                		if(l[k].time() > longestTime)
                			longestTime = (long) l[k].time();
                			
                		moneyEarned += ((l[k].time()/3600000) * a.rate);
                	}
                }
              	
              	
          	}

          }
    	  
    	  
    	  if(projects.length > 0)
          {
          	for(int i = 0; i < projects.length; i++)
          	{
              	Project a = projects[i];
              	Expense[] e = a.getExpenses(this);
              	totalExpenses+= e.length;
              	if(e.length > 0)
                {
                	for(int j = 0; j < e.length; j++)
                	{
                		expenseCost+=e[i].cost;
                	}
                }
              	
              	Log[] l = a.getLogs(this);
              	totalLogs += l.length;
              	if(l.length > 0)
                {
                	for(int k = 0; k < l.length; k++)
                	{
                		totalTime += l[k].time();
                		if(l[k].time() > longestTime)
                			longestTime = (long) l[k].time();
                			
                		moneyEarned += ((l[k].time()/3600000) * a.rate);
                	}
                }
              	
              	
          	}

          }
    	  
    	  
    	DecimalFormat twoDForm = new DecimalFormat("#.##");
    	  
    	money.setText("$"+twoDForm.format(moneyEarned));
    	expenses.setText("$"+twoDForm.format(expenseCost));
    	total.setText("$"+twoDForm.format(moneyEarned+expenseCost));
    	
    	totalwork.setText(""+millisToTime(totalTime));
    	float avgtimeF = (float) totalTime/totalLogs;
    	long avgtime = (long) avgtimeF;
    	avgwork.setText(""+millisToTime(avgtime));
    	longwork.setText(""+millisToTime(longestTime));
    	
    	float timeHrs = (float) totalTime/3600000;
    	float avgRate = (float) moneyEarned / timeHrs;
    	if(avgRate > 0)
    		hourly.setText("$"+twoDForm.format(avgRate));
    	else
    		hourly.setText("$0.00");
    	
    	clients_view.setText(""+clients.length);
    	projects_view.setText(""+projects.length);
    	logs_view.setText(""+totalLogs);
    	expenses_view.setText(""+totalExpenses);
    	
    	
    }
   
   public String millisToTime(long millis)
   {
	    int minutes = (int) ((millis / 1000) / 60);
	    int hours = minutes / 60;
	    minutes = minutes % 60;
		
		return String.format("%d:%02d", hours, minutes);
   }
   
    /*Create Menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.clients_projects_menu, menu);
        return true;
    }
    
    
    //Respond to Menu actions
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.swapItems:
        	String newTitle = ShowItems();
        	item.setTitle(newTitle);
            return true;
        case R.id.settings:
        	//coming soon...
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    //Filtering Util
    public String ShowItems()
    {
    	View clients = findViewById(R.id.clients);
        View projects = findViewById(R.id.projects);
        
        if(clients.getVisibility() == 8)
        {
        	clients.setVisibility(0);
            projects.setVisibility(8);
            Toast.makeText(getApplicationContext(), "Showing Clients", Toast.LENGTH_SHORT).show();
            return "Show Projects";
        }
        else
        {
        	clients.setVisibility(8);
            projects.setVisibility(0);
            Toast.makeText(getApplicationContext(), "Showing Projects", Toast.LENGTH_SHORT).show();
            return "Show Clients";
        }
        
    }*/
    
    //Launch Project Menu
    public View.OnClickListener launch_projectMenu = new View.OnClickListener()
    {
    	 public void onClick(View v) {
    		 
    		 projectMenu((Integer) v.getTag());
    		 
         }
    
    };
    
    //Create Project Menu
    public void projectMenu(int id)
    {
    	final CharSequence[] items = {"Clock In", "Clock in at...", "Enter Expenses", "Export", "Details", "Edit", "Delete"};
    	final Project project = projects[id];
    	
    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setItems(items, new DialogInterface.OnClickListener() {
       	    public void onClick(DialogInterface dialog, int item) {
       	    	if(items[item] == "Clock In")
       	    	{
       	    		clockIn(project);
       	    	}
       	    	if(items[item] == "Clock in at...")
       	    	{
       	    		clockInAt(project);
       	    	}
       	    	if(items[item] == "Enter Expenses")
       	    	{
       	    		enterExpenses(project);
       	    	}
       	    	if(items[item] == "Details")
       	    	{
       	    		showLogs(project);
       	    	}
       	    	if(items[item] == "Delete")
       	    	{
       	    		String toastMsg;
       	    		if(project.delete(HoursTrackerActivity.this))
       	    		{
       	    			toastMsg = "Project Deleted";
       	    			reloadList();
       	    		}
       	    		else
       	    		{
       	    			toastMsg = "Failed to delete project. Try again.";
       	    		}
       	    		Toast.makeText(getApplicationContext(), toastMsg, Toast.LENGTH_SHORT).show();
       	    	}
       	    	if(items[item] == "Export")
       	    	{
       	    		Intent i = new Intent(HoursTrackerActivity.this, ExportProjectActivity.class);
       	    		i.putExtra("project_id", project.id);
       	    		startActivityForResult(i, 1);
       	    	}
       	    	if(items[item] == "Edit")
       	    	{
       	    		Intent i = new Intent(HoursTrackerActivity.this, EditProjectActivity.class);
       	    		i.putExtra("project_id", project.id);
       	    		startActivityForResult(i, 1);
       	    	}
       	    }
       	});
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle(project.name);
        alert.show();
    }
    
    //Launch Client Menu
    public View.OnClickListener launch_clientMenu = new View.OnClickListener()
    {
    	 public void onClick(View v) {
    		 
    		 clientMenu((Integer) v.getTag());
    		 
         }
    
    };
    
    //Create Client Menu
    public void clientMenu(int id)
    {
    	final CharSequence[] items = {"Clock In", "Clock in at...", "Enter Expenses", "Details", "Edit", "Delete Client and Projects"};
    	final Client client = clients[id];
    	
    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setItems(items, new DialogInterface.OnClickListener() {
       	    public void onClick(DialogInterface dialog, int item) {
       	    	if(items[item] == "Clock In")
       	    	{
       	    		clockIn(client);
       	    	}
       	    	if(items[item] == "Clock in at...")
       	    	{
       	    		clockInAt(client);
       	    	}
       	    	if(items[item] == "Enter Expenses")
       	    	{
       	    		enterExpenses(client);
       	    	}
       	    	if(items[item] == "Details")
       	    	{
       	    		showLogs(client);
       	    	}
       	    	if(items[item] == "Delete Client and Projects")
       	    	{
       	    		deleteClient(client);
       	    	}
       	    	if(items[item] == "Edit")
       	    	{
       	    		Intent i = new Intent(HoursTrackerActivity.this, EditClientActivity.class);
       	    		i.putExtra("client_id", client.id);
       	    		startActivityForResult(i, 1);
       	    	}
       	    	/*if(items[item] == "View Projects")
       	    	{
       	    		Intent i = new Intent(HoursTrackerActivity.this, ProjectsForActivity.class);
       	    		i.putExtra("client_id", client.id);
       	    		startActivityForResult(i, 2);
       	    	}*/
       	    }
       	});
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle(client.company);
        alert.show();
    }
    
    //Clock In Client
    public void clockIn(Client client)
    {
    	long time = (long) (System.currentTimeMillis());
    	
    	Intent i = new Intent(HoursTrackerActivity.this, ClockedInActivity.class);
   		i.putExtra("client_id", client.id);
   		i.putExtra("start_time", time);
   		startActivityForResult(i, 2);
    }
    
    //Clock In Project
    public void clockIn(Project project)
    {
    	long time = (long) (System.currentTimeMillis());
    	
    	Intent i = new Intent(HoursTrackerActivity.this, ClockedInActivity.class);
   		i.putExtra("project_id", project.id);
   		i.putExtra("start_time", time);
   		startActivityForResult(i, 2);
    }
    
    public void clockInAt(Project project)
    {
    	clockInAt_Project = project;
    	final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mMonth = c.get(Calendar.MONTH) + 1;
        mYear = c.get(Calendar.YEAR);

    	showDialog(TIME_DIALOG_ID);
    }
    
    public void clockInAt(Client client)
    {
    	clockInAt_Client = client;
    	final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mMonth = c.get(Calendar.MONTH) + 1;
        mYear = c.get(Calendar.YEAR);

    	showDialog(TIME_DIALOG_ID);
    }
    
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }
    
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    	    new TimePickerDialog.OnTimeSetListener() {
    	        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    	            mHour = hourOfDay;
    	            mMinute = minute;
    	            String dateString = ""+mMonth+"/"+mDay+"/"+mYear+" "+mHour+":"+mMinute; 
    	             
    	            Date date;
    	            DateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:m");
    	            try{
    	                date = (Date)formatter.parse(dateString);
    	                long time = date.getTime();
    	             
    	                Intent i = new Intent(HoursTrackerActivity.this, ClockedInActivity.class);
    	                if(clockInAt_Project != null)
    	                {
    	                	i.putExtra("project_id", clockInAt_Project.id);
    	                }
    	                else
    	                {
    	                	i.putExtra("client_id", clockInAt_Client.id);
    	                }
    	           		i.putExtra("start_time", time);
    	           		startActivityForResult(i, 2);
    	            }
    	            catch(Exception e)
    	            {
    	            	Toast.makeText(getApplicationContext(), "Failed to Clock out.", Toast.LENGTH_LONG).show();
    	            }
    	        }

    	    };
    
    
    //Enter Expenses Client
    public void enterExpenses(Client client)
    {
    	Intent i = new Intent(HoursTrackerActivity.this, EnterExpensesActivity.class);
   		i.putExtra("client_id", client.id);
   		startActivityForResult(i, 2);
    }
    
    //Enter Expenses Project
    public void enterExpenses(Project project)
    {
    	Intent i = new Intent(HoursTrackerActivity.this, EnterExpensesActivity.class);
   		i.putExtra("project_id", project.id);
   		startActivityForResult(i, 2);
    }
    	    
    //Show Logs Project
    public void showLogs(Project project)
    {	
    	Intent i = new Intent(HoursTrackerActivity.this, ViewLogsActivity.class);
   		i.putExtra("project_id", project.id);
   		startActivityForResult(i, 2);
    }
    
    //Show Logs Client
    public void showLogs(Client client)
    {	
    	Intent i = new Intent(HoursTrackerActivity.this, ViewLogsActivity.class);
   		i.putExtra("client_id", client.id);
   		startActivityForResult(i, 2);
    }
    
    //Delete Client
    public void deleteClient(Client passed_client)
    {
    	final CharSequence[] options = {"Yes", "No"};
    	final Client client = passed_client;
    	
    	AlertDialog.Builder yes_no = new AlertDialog.Builder(this);
   		yes_no.setItems(options, new DialogInterface.OnClickListener() {
       	    public void onClick(DialogInterface dialog, int choice) {
       	    	if(options[choice] == "Yes")
       	    	{
       	    		String toastMsg;
       	    		if(client.delete(HoursTrackerActivity.this))
       	    		{
       	    			toastMsg = "Client and Projects Deleted";
       	    			reloadList();
       	    		}
       	    		else
       	    		{
       	    			toastMsg = "Failed to delete client. Try again.";
       	    		}
       	    		Toast.makeText(getApplicationContext(), toastMsg, Toast.LENGTH_SHORT).show();
       	    	}
       	    }
       	 });
   	 AlertDialog sub_alert = yes_no.create();
       // Title for AlertDialog
       sub_alert.setTitle("Are You Sure?");
       sub_alert.show();
    	
    }
    
    //Create Add Menu
    public void createMenu(View view) {
    	
    	final CharSequence[] items = {"New Client", "New Project"};
    	
    	 AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
         alt_bld.setItems(items, new DialogInterface.OnClickListener() {
        	    public void onClick(DialogInterface dialog, int item) {
        	    	if(items[item] == "New Client")
        	    	{
        	    		Intent i = new Intent(HoursTrackerActivity.this, CreateClientActivity.class);
        	    		startActivityForResult(i, 0);
        	    	}
        	    	else
        	    	{
        	    		Intent i = new Intent(HoursTrackerActivity.this, CreateProjectActivity.class);
        	    		startActivityForResult(i, 0);
        	    	}
        	    }
        	});
         AlertDialog alert = alt_bld.create();
         // Title for AlertDialog
         alert.setTitle("Create:");
         alert.show();
    
    }

    public void reloadList()
    {
	   	 Intent i = getBaseContext().getPackageManager()
	   	 .getLaunchIntentForPackage(getBaseContext().getPackageName() );
	
	   	 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
	   	 startActivity(i);
    }
    
    protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {	 
    	reloadList();
    }
    
}